package org.hospital.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hospital.entity.Client;
import org.hospital.entity.ClientDAO;
import org.hospital.entity.Department;
import org.hospital.entity.DepartmentDAO;
import org.hospital.entity.Employee;
import org.hospital.entity.EmployeeDAO;
import org.hospital.entity.Position;
import org.hospital.entity.PositionDAO;
import org.hospital.service.UserService;
import org.hospital.util.DateUtil;
import org.hospital.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private EmployeeDAO eDao;
	@Autowired
	private ClientDAO cDao;
	@Autowired
	private PositionDAO psDao;
	@Autowired
	private DepartmentDAO dpDao;

	@Override
	public Employee getEmployeeById(int employeeId) {
		return eDao.findById(employeeId);
	}

	@Override
	public String addEmployee(Employee entity) {
		List<Employee> eList = eDao.findByAccount(entity.getAccount());
		if (null != eList && eList.size() != 0) {
			return StringUtil.setResult(411, "账号重复", "");
		}

		eList = eDao.findByEmployeeCode(entity.getEmployeeCode());
		if (null != eList && eList.size() != 0) {
			return StringUtil.setResult(412, "工号重复", "");
		}

		Employee e = eDao.merge(entity);
		if (e == null) {
			return StringUtil.setResult(501, "数据库插入错误", "");
		}
		return StringUtil.setResult(200, "操作成功", e);
	}

	@Override
	public String saveEmployee(Employee entity) {
		// 获取数据库原始对象
		Employee employee = eDao.findById(entity.getEmployeeId());
		if (employee == null) {
			return StringUtil.setResult(403, "employeeId找不到", "");
		}

		/*
		 * System.out.println("UserServiceImpl.saveEmployee.employee1 : " +
		 * JSON.toJSONString(employee, true));
		 * System.out.println("UserServiceImpl.saveEmployee.entity : " +
		 * JSON.toJSONString(entity, true));
		 */

		// 检测employeeCode 有效性
		if (!employee.getEmployeeCode().equals(entity.getEmployeeCode())) {
			List<Employee> eList = eDao.findByEmployeeCode(entity.getEmployeeCode());
			if (null != eList && eList.size() != 0) {
				return StringUtil.setResult(412, "工号重复", "");
			}
		}

		// 赋予不需修改字段
		entity.setAccount(employee.getAccount());
		entity.setPwd(employee.getPwd());
		entity.setStatus(employee.getStatus());
		entity.setRegisterTime(employee.getRegisterTime());
		entity.setLoginIp(employee.getLoginIp());
		entity.setLoginTime(employee.getLoginTime());
		employee = eDao.merge(entity);
		if (employee == null) {
			return StringUtil.setResult(501, "数据库操作错误", "");
		}
		/*
		 * System.out.println("UserServiceImpl.saveEmployee.employee2 : " +
		 * JSON.toJSONString(employee, true));
		 */
		return StringUtil.setResult(200, "操作成功", employee);
	}

	@Override
	public Client getClentById(int clientId) {
		return cDao.findById(clientId);
	}

	@Override
	public Client addClient(Client client) {
		return cDao.merge(client);
	}

	@Override
	public Client saveClient(Client client) {
		return cDao.merge(client);
	}

	@Override
	public Employee getEmployeeByCode(String employeeCode) {
		List<Employee> eList = eDao.findByEmployeeCode(employeeCode);
		if (null == eList || eList.size() == 0) {
			return null;
		}
		return eList.get(0);
	}

	@Override
	public List<Employee> getClinicPrincipal() {
		return eDao.getClinicPrincipal();
	}

	@Override
	public List<Employee> getDepartmentPrincipal(int clinicId) {
		return eDao.getDepartmentPrincipal(clinicId);
	}

	@Override
	public JSONArray getEmployeeData(int clinicId,int pageIndex,int pageSize) {
		List<Employee> eList = eDao.getEmployeeData(clinicId,pageIndex,pageSize);
		return formatEmployeeList(eList);
	}

	@Override
	public JSONArray getSearchEmployeeData(int clinicId, String search) {
		List<Employee> eList = eDao.getSearchEmployeeData(clinicId, search);
		return formatEmployeeList(eList);
	}

	@Override
	public JSONArray getClinicEmployee(int clinicId) {

		Map paramMap = new HashMap<>();
		paramMap.put("clinicId", clinicId);
		paramMap.put("isOnJob", 0);
		List<Employee> eList = eDao.getByProperties(paramMap);

		return formatEmployeeList(eList);
	}

	@Override
	public JSONArray getDepartmentEmployee(int departmentId) {
		Map paramMap = new HashMap<>();
		paramMap.put("departmentId", departmentId);
		paramMap.put("isOnJob", 0);
		List<Employee> eList = eDao.getByProperties(paramMap);

		return formatEmployeeList(eList);
	}

	private JSONArray formatEmployeeList(List<Employee> eList) {
		JSONArray resultJA = new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < eList.size(); i++) {
			JSONObject resultOJ = new JSONObject();
			Position po = psDao.findById(eList.get(i).getPositionId());
			Department dp = dpDao.findById(eList.get(i).getDepartmentId());
			resultOJ.put("name", eList.get(i).getName());
			resultOJ.put("age", eList.get(i).getAge());
			resultOJ.put("employeeCode", eList.get(i).getEmployeeCode());
			resultOJ.put("employeeId", eList.get(i).getEmployeeId());
			resultOJ.put("positionId", eList.get(i).getPositionId());
			resultOJ.put("positionName", po.getName());
			resultOJ.put("departmentId", eList.get(i).getDepartmentId());
			resultOJ.put("departmentName", dp.getName());
			resultOJ.put("account", eList.get(i).getAccount());
			resultOJ.put("pwd", eList.get(i).getPwd());
			resultOJ.put("loginIp", eList.get(i).getLoginIp());
			
			if(eList.get(i).getSex() == null) {
				resultOJ.put("sex", "");
			}else if (eList.get(i).getSex().equals("0")) {
				resultOJ.put("sex", "男");
			} else if (eList.get(i).getSex().equals("1")) {
				resultOJ.put("sex", "女");
			}
			resultOJ.put("sexInt", eList.get(i).getSex());
			
			resultOJ.put("registerTime", sdf.format(eList.get(i).getRegisterTime()));
			if (eList.get(i).getLoginTime() != null) {
				resultOJ.put("loginTime", sdf.format(eList.get(i).getLoginTime()));
			}
			if (eList.get(i).getBirthdate() != null) {
				resultOJ.put("birthdate", sdf.format(eList.get(i).getBirthdate()));
				//System.out.println("formatEmployeeList.long : "+DateUtil.getShortDateLong(sdf.format(eList.get(i).getBirthdate())));
				//System.out.println("formatEmployeeList.equal : " + ("1000-01-01".equals(sdf.format(eList.get(i).getBirthdate()))));
			}
			
			if (eList.get(i).getStatus() == 0) {
				resultOJ.put("status", "在线");
			} else if (eList.get(i).getStatus() == 1) {
				resultOJ.put("status", "不在线");
			} else if (eList.get(i).getStatus() == -1) {
				resultOJ.put("status", "禁止登录");
			}
			resultOJ.put("statusInt", eList.get(i).getStatus());
			
			if (eList.get(i).getIsOnJob() == 0) {
				resultOJ.put("isOnJob", "在职");
			} else if (eList.get(i).getIsOnJob() == 1) {
				resultOJ.put("isOnJob", "离职");
			}
			resultOJ.put("isOnJobInt", eList.get(i).getIsOnJob());
			
			if (eList.get(i).getIsDomain() == 0) {
				resultOJ.put("isDomain", "允许跨域访问");
			} else if (eList.get(i).getIsDomain() == 1) {
				resultOJ.put("isDomain", "不允许跨域访问");
			}
			resultOJ.put("isDomainInt", eList.get(i).getIsDomain());
			
			resultJA.add(resultOJ);
		}
		return resultJA;
	}

	@Override
	public JSONArray getClientByIdentity(String identity) {
		List<Client> cList = cDao.findByIdentity(identity);
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < cList.size(); i++) {
			Client client = cList.get(i);
			JSONObject resultJO = new JSONObject();
			resultJO.put("clientId", client.getClientId());
			resultJO.put("name", client.getName());
			resultJO.put("identity", client.getIdentity());
			resultJO.put("phone", client.getPhone());
			resultJO.put("sex", client.getSex());
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	@Override
	public List<Client> isRepeatIdentity(String identity) {
		return cDao.findByIdentity(identity);
	}

	@Override
	public JSONArray getAllClient(int pageIndex,int pageSize) {
		List<Client> cList=cDao.getAllClient(pageIndex, pageSize);
		JSONArray resultJA=new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i=0;i<cList.size();i++){
			JSONObject resultJO=new JSONObject();
			resultJO.put("clientId", cList.get(i).getClientId());
			resultJO.put("name", cList.get(i).getName());
			resultJO.put("identity", cList.get(i).getIdentity());
			if(cList.get(i).getSex()==0){
				resultJO.put("sex", "男");
			}else if(cList.get(i).getSex()==1){
				resultJO.put("sex", "女");
			}
			resultJO.put("sexId", cList.get(i).getSex());
			resultJO.put("phone", cList.get(i).getPhone());
			if(cList.get(i).getBirthdate() == null) {
				resultJO.put("birthdate", "");
			}else {
				resultJO.put("birthdate", sdf.format(cList.get(i).getBirthdate()));
			}
			resultJO.put("nation", cList.get(i).getNation());
			if(cList.get(i).getMarriage() == null) {
				resultJO.put("marriage", "未记录");
			}else {
				if(cList.get(i).getMarriage() == 0){
					resultJO.put("marriage", "未婚");
				}else if(cList.get(i).getMarriage() == 1){
					resultJO.put("marriage", "已婚");
				}
			}
			
			resultJO.put("marriageId", cList.get(i).getMarriage());
			resultJO.put("occupation", cList.get(i).getOccupation());
			resultJO.put("registerTime", sdf.format(cList.get(i).getRegisterTime()));
			if(cList.get(i).getStatus()==-1){
				resultJO.put("status", "黑名单客户");
			}else if(cList.get(i).getStatus()==0){
				resultJO.put("status", "普通客户");
			}else if(cList.get(i).getStatus()==1){
				resultJO.put("status", "普通会员客户");
			}else if(cList.get(i).getStatus()==2){
				resultJO.put("status", "超级会员客户");
			}
			resultJO.put("statusId", cList.get(i).getStatus());
			resultJA.add(resultJO);
		}
		return resultJA;	
	}
	
	@Override
	public Long getClientNumber() {
		return cDao.getClientNumber();
	}
	
	@Override
	public JSONArray searchClientByName(String name) {
		JSONArray resultJA = new JSONArray();
		List<Client> cList = cDao.searchByName(name);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < cList.size(); i++) {
			Client client = cList.get(i);
			JSONObject resultJO = new JSONObject();
			resultJO.put("name", client.getName());
			resultJO.put("identity", client.getIdentity());
			resultJO.put("clientId", client.getClientId());
			Date birthdate = new Date(client.getBirthdate().getTime());
			resultJO.put("birthdate", sdf.format(birthdate));
			if(client.getSex().equals(0)) {
				resultJO.put("sex","男");
				resultJO.put("sexInt",0);
			}
			if(client.getSex().equals(1)) {
				resultJO.put("sex","女");
				resultJO.put("sexInt",1);
			}
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	@Override
	public Long getClinicEmployeeNumber(int clinicId) {
		return eDao.getClinicEmployeeNumber(clinicId);
	}

	

}
